package com.twelve.all.utils;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.twelve.all.R;
import com.twelve.all.bean.ClearBottom;

import org.greenrobot.eventbus.EventBus;


/**
 * Created by 张龙臣 on 2018/10/25.
 */

public class LoadingDialog extends Dialog {

    private TextView mTvMsg;

    public LoadingDialog(@NonNull Context context) {
        this(context, null);
    }

    @SuppressLint("SetTextI18n")
    public LoadingDialog(@NonNull Context context, String msg) {
        super(context, R.style.generic_dialog);
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_loading, null);
        mTvMsg = (TextView) v.findViewById(R.id.tvMsg);
        if (!TextUtils.isEmpty(msg)) {
            mTvMsg.setVisibility(View.VISIBLE);
            mTvMsg.setText(msg + "...");
        } else {
            mTvMsg.setText("加载中...");
        }
        setCancelable(false);// 不可以用“返回键”取消
        setCanceledOnTouchOutside(false);
        setContentView(v, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
    }

    @SuppressLint("SetTextI18n")
    public void setMsg(String msg) {
        if (TextUtils.isEmpty(msg))
            return;
        if (mTvMsg.getVisibility() == View.GONE)
            mTvMsg.setVisibility(View.VISIBLE);
        mTvMsg.setText(msg + "...");
    }

    public void dismissMsg(){
        mTvMsg.setVisibility(View.GONE);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        EventBus.getDefault().post(new ClearBottom());
    }
}
